/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.utils;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import org.apache.axis.EngineConfiguration;
import org.apache.axis.client.Service;

import com.cqcis.uip.base.framework.core.RegisteredService;
import com.cqcis.uip.base.framework.service.FileProviderAdaptor;

/**
 * 修改webservice客户端自动生成代码的 serviceLocator, 增加日志记录功能
 * 
 * @author huangmy
 * @date   2008-11-4
 */
public class WSClientRecordServiceUtil {
	/**
	 * 产生对象
	 * 
	 * @param servicLocatorClazz
	 * @return
	 */
    public static Service create(Class<? extends Service> servicLocatorClazz, final RegisteredService currentRS) {
    	Enhancer en = new Enhancer();
		
		en.setSuperclass(servicLocatorClazz);
		en.setCallback(new MethodInterceptor() {
			public Object intercept(Object o, Method method, Object[] args,
					MethodProxy proxy) throws Throwable {
				if ("getEngineConfiguration".equals(method.getName())
						&& EngineConfiguration.class.isAssignableFrom(method.getReturnType())) {
					return new FileProviderAdaptor(currentRS);
				}
				
				return proxy.invokeSuper(o, args);
			}
		});
		
		CallbackFilter filter = new CallbackFilter() {
			public int accept(Method method) {
				return Modifier.isProtected(method.getModifiers()) ? 0 : 0;
			}
		};
		
		en.setCallbackFilter(filter);
		
		return Service.class.cast(en.create());
    }
}
